package com.xdwanj.util;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.xdwanj.dao.BaseDao;
import org.springframework.jdbc.core.JdbcTemplate;

import java.beans.PropertyVetoException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class JdbcUtil {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    private static JdbcTemplate jdbcTemplate = new JdbcTemplate();

    public static JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    static {
        InputStream is = JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties");

        Properties pro = new Properties();
        try {
            pro.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }

        driver = pro.getProperty("driver");
        url = pro.getProperty("url");
        username = pro.getProperty("username");
        password = pro.getProperty("password");

        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        try {
            dataSource.setDriverClass(driver);
            dataSource.setJdbcUrl(url);
            dataSource.setUser(username);
            dataSource.setPassword(password);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }

        jdbcTemplate.setDataSource(dataSource);
    }
}
